<?php

declare (strict_types=1);
/**
 * @copyright 深圳市易果网络科技有限公司
 * @version 1.0.0
 * @link https://dayiguo.com
 */

namespace App\Controller\user;

use App\Dao\UserCouponDao;
use App\Middleware\User\UserMiddleware;

use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\HttpServer\Annotation\Middleware;

/**
 * 卷包控制器
 *
 * @AutoController()
 * @Middleware(UserMiddleware::class)
 * @author 张昊(2396675557@qq.com)
 * @package App\Controller\admin
 */
class UserCouponController extends Controller
{
    /**
     * @Inject
     * @var UserCouponDao
     */
    private $dao;

    /**
     * 获取用户券包
     *
     * @author 张昊(2396675557@qq.com)
     */
    public function get()
    {
        $status = (int)$this->request->input('status', 0);
        $type = (int)$this->request->input('type', 0);

        // 状态筛选
        $statusList = [
            // 可用优惠券
            1 => ['status' => $this->dao::STATUS_PAID,'use_time' => 0, 'expire_time' => ['>', time()]],
            // 已使用
            2 => ['use_time' => ['>', 0]],
            // 已过期
            3 => ['use_time' => 0, 'expire_time' => ['<', time()]]
        ];
        $map = $statusList[$status] ?? [];

        // 优惠券类型，1=折扣券，2=现金抵用券
        $type > 0 && $map['type'] = $type;

        $result = $this->dao->get($map);

        $this->success($result);
    }

    /**
     *  优惠券详情
     *
     * @author 张昊(2396675557@qq.com)
     */
    public function details()
    {
        $id = (int)$this->request->input('id', 0);

        $result = $this->dao->details( $id );

        if( !$result ) {
            $this->error();
        }

        $result['rebate'] = $result['coupon']['goods_price'] / $result['coupon']['price'];

        $this->success($result);
    }

    /**
     * 查看优惠券核销码
     *
     * @author 张昊(2396675557@qq.com)
     */
    public function getQrCode()
    {
        $id = (int)$this->request->input('id', 0);

        $result = $this->dao->getQrCode( $id );

        $this->success($result);
    }
}